package person.twj.jwt.core.security.handler;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import person.twj.jwt.domain.vo.RsVo;

import java.io.IOException;

/**
 * 未登录返回<br>
 * 主要功能：<br>
 *  1、没有登录的用户，不能访问该系统
 */
@Slf4j
public class MyNotLoginAccessHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {

        if(!InsufficientAuthenticationException.class.isAssignableFrom(e.getClass())){
            log.debug("{}",e);
            RsVo.failed(500, e.getMessage()).writeTo(response);
            return;
        }

        RsVo.failed(403, "权限不足").writeTo(response);
    }
}
